<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Platformmsg extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit,$where) = $this->buildTableParames();
            $where2[]=['is_del','=',0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where2[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['send_time'])) {
                    $create_time = explode(' - ', $filter['send_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where2[] = ['send_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('platform_msg')
                ->page($page, $limit)
                ->where($where)
                ->where($where2)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time']=date('Y-m-d H:i:s',$_list[$k]['create_time']);
                $msg_cate=Db::name('message_cate')->find($v['msg_cate_id']);
                $_list[$k]['msg_cate']=$msg_cate['name'];
                if( $_list[$k]['send_time']){
                    $_list[$k]['send_time']=date('Y-m-d H:i:s',$_list[$k]['send_time']);
                }else{
                    $_list[$k]['send_time']='未发送';
                }
            }
            $total = Db::name('platform_msg') ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $insert_data = array(
                'title' => $data['title'],
                'content' => $data['content'],
                'is_time' => $data['is_time'],
                'msg_cate_id' => $data['msg_cate'],
                'create_time'=>time(),
                'get_user_id'=>0
            );
            if ($data['is_time']==1){
                if ($data['send_time']==null){
                    $this->error('请输入发送时间！');
                }
                $insert_data['send_time']=strtotime($data['send_time']);
            }else{
                $insert_data['is_send']=1;
                $insert_data['send_time']=time();
            }
            $stmt = Db::name('platform_msg')->insertGetId($insert_data);
            $this->success('操作成功！');
        }
        //发送消息类型
        $msg_cate=Db::name('message_cate')
            ->where([
                'is_del'=>0,
            ])->where('id','<>',4)
            ->order('sort desc')
            ->select();
        $this->assign('msg_cate',$msg_cate);
        return $this->fetch();

    }

    public function edit(){
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'title' => $data['title'],
                'content' => $data['content'],
                'is_time' => $data['is_time'],
                'msg_cate_id' => $data['msg_cate'],
                'get_user_id'=>0
            );
            if ($data['is_time']==1){
                if ($data['send_time']==null){
                    $this->error('请输入发送时间！');
                }
                $update_data['send_time']=strtotime($data['send_time']);
            }else{
                $update_data['is_send']=1;
                $update_data['send_time']=time();
            }
            $stmt = Db::name('platform_msg')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        //获取消息
        $data=Db::name('platform_msg')->find($id);
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        //2024-10-23 00:00:00
        //发送消息类型
        $msg_cate=Db::name('message_cate')
            ->where([
                'is_del'=>0
            ])->where('id','<>',4)
            ->order('sort desc')
            ->select();
        $this->assign('data',$data);
        $this->assign('msg_cate',$msg_cate);
        return $this->fetch();
    }

    public function edit2(){
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'title' => $data['title'],
                'content' => $data['content'],
                'is_time' => $data['is_time'],
                'msg_cate_id' => $data['msg_cate'],
                'get_user_id'=>0,
                'create_time'=>time(),
            );
            if ($data['is_time']==1){
                if ($data['send_time']==null){
                    $this->error('请输入发送时间！');
                }
                $update_data['send_time']=strtotime($data['send_time']);
            }else{
                $update_data['is_send']=1;
                $update_data['send_time']=time();
            }
            $stmt = Db::name('platform_msg')->insertGetId($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        //获取消息
        $data=Db::name('platform_msg')->find($id);
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        //2024-10-23 00:00:00
        //发送消息类型
        $msg_cate=Db::name('message_cate')
            ->where([
                'is_del'=>0
            ])
            ->order('sort desc')
            ->select();
        $this->assign('data',$data);
        $this->assign('msg_cate',$msg_cate);
        return $this->fetch();
    }


    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('platform_msg')->where('id', $id)->update([
                    'is_del'=>1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }
}